package com.jy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jy.common.enums.ResponseWrapper;
import com.jy.common.model.search.result.GuessSearchResult;
import com.jy.common.model.search.result.MainPageSearchPageGetInfosResult;
import com.jy.entity.SearchHistory;
import com.jy.entity.Topic;
import com.jy.mapper.SearchHistoryMapper;
import com.jy.mapper.TopicMapper;
import com.jy.service.TopicService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 话题 服务实现类
 * </p>
 *
 * @author fzg
 * @since 2024-11-12
 */
@Service
public class TopicServiceImpl extends ServiceImpl<TopicMapper, Topic> implements TopicService {

    @Autowired
    private TopicMapper topicMapper;

    @Autowired
    private SearchHistoryMapper searchHistoryMapper;

    @Override
    public ResponseWrapper getSearchAllInfo(Integer userAid) {

        // 返回结果
        MainPageSearchPageGetInfosResult result = new MainPageSearchPageGetInfosResult();

        // 根据userAid 查询用户的搜索历史(查询最新的12条)
        List<SearchHistory> searchHistories = searchHistoryMapper.selectList(
                new LambdaQueryWrapper<SearchHistory>()
                        .eq(SearchHistory::getUserAid, userAid)
                        .orderByDesc(SearchHistory::getCreateTime)
                        .last("limit 12")
        );
        result.setSearchHistories(searchHistories);

        // 猜你想搜（自己购买的东西，自己搜索的最多的）
        List<SearchHistory> guessSearchList = searchHistoryMapper.selectList(
                new LambdaQueryWrapper<SearchHistory>()
                        .eq(SearchHistory::getUserAid, userAid)
                        .orderByDesc(SearchHistory::getCreateTime)
                        .orderByDesc(SearchHistory::getCount)
                        .last("limit 10")
        );
        ArrayList<GuessSearchResult> guessSearchResults = new ArrayList<>();
        if(guessSearchList.isEmpty()){

            // 搜索最多的话题和最多的搜索记录各五条
            List<SearchHistory> mostFiveHistory = searchHistoryMapper.selectList(
                new LambdaQueryWrapper<SearchHistory>()
                    .orderByDesc(SearchHistory::getCreateTime)
                    .orderByDesc(SearchHistory::getCount)
                    .last("limit 5")
            );
            mostFiveHistory.forEach(searchHistory -> {
                GuessSearchResult guessSearchResult = new GuessSearchResult();
                guessSearchResult.setName(searchHistory.getName());
                guessSearchResult.setHaveLabel(searchHistory.getHaveLabel());
                guessSearchResult.setLabelName(searchHistory.getLabelName());
                guessSearchResults.add(guessSearchResult);
            });
            List<Topic> mostFiveTopics = topicMapper.selectList(
                new LambdaQueryWrapper<Topic>()
                    .orderByDesc(Topic::getCreateTime)
                    .eq(Topic::getHaveLabel,true)
                    .last("limit 5")
            );
            mostFiveTopics.forEach(topic -> {
                GuessSearchResult guessSearchResult = new GuessSearchResult();
                guessSearchResult.setName(topic.getName());
                guessSearchResult.setHaveLabel(topic.getHaveLabel());
                guessSearchResult.setLabelName(topic.getLabelName());
                guessSearchResults.add(guessSearchResult);
            });
        }else {
            guessSearchList.forEach(searchHistory -> {
                GuessSearchResult guessSearchResult = new GuessSearchResult();
                guessSearchResult.setName(searchHistory.getName());
                guessSearchResult.setHaveLabel(searchHistory.getHaveLabel());
                guessSearchResult.setLabelName(searchHistory.getLabelName());
                guessSearchResults.add(guessSearchResult);
            });
        }
        result.setGuessSearchResults(guessSearchResults);

        // 热门话题
        List<Topic> topics = topicMapper.selectList(new LambdaQueryWrapper<Topic>()
                .orderByDesc(Topic::getCreateTime)
                .last("limit 5")
        );
        result.setTopics(topics);
        return ResponseWrapper.markSuccess(result);
    }
}
